package com.four.bmm.controller;

import com.four.bmm.entity.User;
import com.four.bmm.service.UserService;
import com.four.bmm.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String userpwd=req.getParameter("userpwd");
        User user=new User(username,userpwd);
        UserService userService=new UserService();
        User user1=userService.login(user);
        boolean b=false;
        if(user1==null){
            b=false;
        }else{
            b=true;
        }
        HttpSession session=req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out =resp.getWriter();
        if(b){
            session.setAttribute("loginuser",user1);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }else{
            req.setAttribute("loginmsg","登录失败，用户名与密码不匹配或已被禁用");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
